package srzy.module

import srzy.dao.DBMemory

object DBComp extends AbsCaseDeal with srzy.common.Logging {
  def deal = {

    val dbCompInfos = getCaseInfo.dbCompareInfo
    //    logger.debug("dbCompInfos:" + dbCompInfos)
    for (dbCompInfo <- dbCompInfos) {
      try {
        //        logger.debug("dbCompInfo:" + dbCompInfo)
        val para = dbCompInfo.para
        var arrPara: List[String] = List[String]()
        if (para.indexOf("#") == 0) {
          arrPara = para.substring(1).split("#").toList
        } else {
          arrPara = para.split("#").toList
        }
        var sqlBinded = dbCompInfo.originalSQL
        for (j <- 0 until arrPara.length) {
          if (arrPara.apply(j).matches("[0-9]+")) {
            sqlBinded = sqlBinded.replaceFirst(":s" + (j + 1), arrPara.apply(j));
          } else {
            sqlBinded = sqlBinded.replaceFirst(":s" + (j + 1), "'" + arrPara.apply(j) + "'");
          }
        }
        logger.debug("sql:[" + dbCompInfo.originalSQL + "]")
        logger.debug("PreResult:[" + dbCompInfo.preResult + "]")

        dbCompInfo.assembledSQL = sqlBinded
        logger.debug("sqlBinded:[" + dbCompInfo.assembledSQL + "]")

        dbCompInfo.arrPara = arrPara
        logger.debug("Para:[" + dbCompInfo.para + "]")

        dbCompInfo.actualResult = DBMemory.executeLine(sqlBinded)
        logger.debug("actualResult:[" + dbCompInfo.actualResult + "]")

        if (!(dbCompInfo.preResult == dbCompInfo.actualResult)) {
          dbCompInfo.compareResult = false
          fail
        }
        logger.debug("CompResult:[" + dbCompInfo.compareResult + "]")
      } catch {
        case ex: Exception => {
          logger.error(ex.getMessage(), ex)
          dbCompInfo.compareResult = false
          fail
        }
      }

    }
  }
  def fail = {
    getCaseInfo.dbCompareResult = false
    getCaseInfo.caseResult = false
  }
}